package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import java.text.DecimalFormat;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

public class InfoCPView {
    private static final String CP_KEYS[] = {
            DeviceInfoKey.K.CP_LABEL,
            DeviceInfoKey.K.CP_ABI,
            DeviceInfoKey.K.CP_ABI2,
            DeviceInfoKey.K.CP_CORE_NUM,
            DeviceInfoKey.K.CP_VM_CORE_NUM,
            DeviceInfoKey.K.CP_MIPS,
            DeviceInfoKey.K.CP_MIN_FREQ,
            DeviceInfoKey.K.CP_MAX_FREQ,
            DeviceInfoKey.K.CP_FEATURES,
            DeviceInfoKey.K.CP_IMPLEMENTER,
            DeviceInfoKey.K.CP_ARCHITECTURE,
            DeviceInfoKey.K.CP_VARIANT,
            DeviceInfoKey.K.CP_PART,
            DeviceInfoKey.K.CP_REVISION
    };

    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoCPView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
	public View
	getView(boolean for_preview) {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);
        
        int childno = 0;
        for (int i=0; i<CP_KEYS.length; i++) {
            String info = _di.GetString(CP_KEYS[i]);
            if (!TextUtils.isEmpty(info) && !info.equals(CONSTS.UNAVAILABLE)) {
                View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item,null);
                InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
                row.setTag(wrapper);
                wrapper.setItemName(DeviceInfoKey.GetName(CP_KEYS[i]));
                if (TextUtils.equals(CP_KEYS[i], DeviceInfoKey.K.CP_FEATURES) ||
                    TextUtils.equals(CP_KEYS[i], DeviceInfoKey.K.CP_MIPS)) {
                	// JSON array
    				JSONArray features = _di.GetJSONArray(CP_KEYS[i]);
    				if (features != null && features.length() > 0) {
    				    info = "";
   						for (int j=0; j<features.length(); j++) {
   						    if (j != 0)
   						        info += ", ";
							try {
								info += features.getString(j);
							} catch (JSONException e1) {
							}
    					}
    				}
                }
                else if (TextUtils.equals(CP_KEYS[i], DeviceInfoKey.K.CP_MIN_FREQ) ||
                		TextUtils.equals(CP_KEYS[i], DeviceInfoKey.K.CP_MAX_FREQ)) {
                    JSONArray freqs = _di.GetJSONArray(CP_KEYS[i]);
                    if (freqs != null && freqs.length() > 0) {
                        info = "";
                        for (int j = 0; j < freqs.length(); j++) {
                            if (j != 0)
                                info += ", ";
                            
                            try {
                       			long khz = freqs.getLong(j);
                    			long ghz = khz / InfoViewCONSTS.GHZ_LV; 
                    			if (ghz > 0) {
                    				double mi_v = ((double)khz)/((double)InfoViewCONSTS.GHZ_LV);
                    				info += new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_cp_ghz);
                    			}
                    			else {
                    				double mi_v = ((double)khz)/((double)InfoViewCONSTS.MHZ_LV);
                    				info += new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_cp_mhz);
                    			}
                            } catch (Exception e) {}
                        }
                    }
                }
                wrapper.setItemDetail(info);
                sv.addView(row);
                childno++;
            }               
        }
        return sv;
	}
}
